package cn.edu.scut.fsearch.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scut.kapok.search.SourceRatio;
import cn.edu.scut.fsearch.util.PieChart;
import cn.edu.scut.kapok.search.comm.SearchOption.SourceType;

public class PieChartServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		SourceRatio sourceRatio = (SourceRatio)req.getSession().getAttribute("v4v6");
		if(sourceRatio == null)
		{
//			sourceRatio = SourceRatio.getDefaultSourceRatio(SourceType.v4Andv6);
			sourceRatio = new SourceRatio(0,0,0);
		}
		PieChart pieChart = new PieChart();
		BufferedImage bufferedImage = new BufferedImage(PieChart.WIDTH, PieChart.HEIGHT,BufferedImage.TYPE_INT_RGB); 
		if(pieChart.createPieChart(sourceRatio.getV4(),sourceRatio.getV6(),sourceRatio.getV4Andv6(),bufferedImage))
			ImageIO.write(bufferedImage, PieChart.IMG_FORMAT, resp.getOutputStream()); 
	}

}
